<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Autorepay_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
	/**
	 * 发布文章
	 * @param $data
	 */
	function add($data = array()){
		$this->db->insert('rsh_auto_repay', $data);
		return $this->db->insert_id();
	}
	

	function update($id, $data = array()){
		$this->db->where('id', $id);
		$this->db->update('rsh_auto_repay', $data);
		return $this->db->affected_rows();
	}
    
	function get_auto_repay_list($data){
		//封装查询内容
		$select = "a.*,b.title,b.each_time";
		
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_auto_repay as a left join rsh_borrow as b on a.borrow_id=b.id where a.is_del=0 ";
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['auto_repay_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_auto_repay as a left join rsh_borrow as b on a.borrow_id=b.id where a.is_del=0 ";
		$query = $this->db->query($sql_count);
		$data['auto_repay_count'] = $query->row()->count;
		return $data;
	}
	
	function get_auto_repay_all(){
		$today = time();
		$yestoday = strtotime(date('Y-m-d'));
		$nextday = strtotime(date('Y-m-d 23:59:59'));
 		$sql_result = "SELECT  id,user_id FROM rsh_borrow_repayment 
						 WHERE repayment_time>=$yestoday AND 
						 repayment_time<=$nextday  AND fstatus IN (0,2) 
						 and borrow_id in (select borrow_id from rsh_auto_repay where is_del=0 and fstatus=0) ORDER BY id";
	
    	$query = $this->db->query($sql_result);
		return $query->result_array();
	}
}